package com.example.myworkc;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Fragment weixinBlankFragment = new weixinBlankFragment();
    private Fragment  contactBlankFragment = new contactBlankFragment();
    private Fragment  meBlankFragment = new meBlankFragment();
    private Fragment  findBlankFragment = new findBlankFragment();
    private FragmentManager fragmentManager;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private ImageView imageView1,imageView2,imageView3,imageView4;
    private TextView textView1,textView2,textView3,textView4;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout1=findViewById(R.id.linearout1);
        linearLayout2=findViewById(R.id.linearout2);
        linearLayout3=findViewById(R.id.linearout3);
        linearLayout4=findViewById(R.id.linearout4);
        imageView1=findViewById(R.id.imageView1);
        imageView2=findViewById(R.id.imageView2);
        imageView3=findViewById(R.id.imageView3);
        imageView4=findViewById(R.id.imageView4);
        textView1=findViewById(R.id.textView1);
        textView2=findViewById(R.id.textView2);
        textView3=findViewById(R.id.textView3);
        textView4=findViewById(R.id.textView4);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);
        initframent();

    }
    private void initframent(){
        fragmentManager = getFragmentManager();
        FragmentTransaction transaction =fragmentManager.beginTransaction();
        transaction.add(R.id.id_content,weixinBlankFragment);
        transaction.add(R.id.id_content,contactBlankFragment);
        transaction.add(R.id.id_content,findBlankFragment);
        transaction.add(R.id.id_content,meBlankFragment);
        transaction.commit();
        hideframent(transaction);
    }
    private void hideframent(FragmentTransaction transaction){
        transaction.hide(weixinBlankFragment);
        transaction.hide(findBlankFragment);
        transaction.hide(contactBlankFragment);
        transaction.hide(meBlankFragment);
    }


    private void showfragment(int i) {
        FragmentTransaction transaction =fragmentManager.beginTransaction();
        hideframent(transaction);
        switch (i){
            case 0:
                transaction.show(weixinBlankFragment);
                break;
            case 1:
                transaction.show(contactBlankFragment);
                break;
            case 2:
                transaction.show(findBlankFragment);
                break;
            case 3:
                transaction.show(meBlankFragment);
                break;
            default:
                break;

        }
        transaction.commit();


    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onClick(View view) {
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideframent(transaction);
        setcolor();
        switch (view.getId()){
            case R.id.linearout1:
                showfragment(0);
                imageView1.setColorFilter(Color.GREEN);
                textView1.setTextColor(Color.GREEN);
                break;
            case R.id.linearout2:
                imageView2.setColorFilter(Color.GREEN);
                showfragment(1);
                textView2.setTextColor(Color.GREEN);
                break;
            case R.id.linearout3:
                imageView3.setColorFilter(Color.GREEN);
                showfragment(2);
                textView3.setTextColor(Color.GREEN);
                break;
            case R.id.linearout4:
                imageView4.setColorFilter(Color.GREEN);
                showfragment(3);
                textView4.setTextColor(Color.GREEN);
                break;
            default:
                break;

        }

    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setcolor() {
        imageView1.setColorFilter(Color.blue(200));
        imageView2.setColorFilter(Color.blue(200));
        imageView3.setColorFilter(Color.blue(200));
        imageView4.setColorFilter(Color.blue(200));
        textView1.setTextColor(Color.WHITE);
        textView2.setTextColor(Color.WHITE);
        textView3.setTextColor(Color.WHITE);
        textView4.setTextColor(Color.WHITE);

    }


}


